package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.tamartech.greendb.loader.ElectoralRollLoader;

public class ElectoralRollImporter_old
{
    private static Logger logger = Logger.getLogger (ElectoralRollImporter_old.class);
    private static SimpleDateFormat formatRollDate = new SimpleDateFormat ("dd/MM/yyyy");
    private static SimpleDateFormat format18thDate = new SimpleDateFormat ("dd/MM/yyyy");
    
    private static final int COL_REF1 = 0;
    private static final int COL_REF2 = 1;
    private static final int COL_ROW_TYPE = 2;
    private static final int COL_BIRTHDAY_18 = 3;
    private static final int COL_SURNAME = 4;
    private static final int COL_FIRST_NAMES = 5;
    private static final int COL_ADDRESS1 = 6;
    private static final int COL_ADDRESS2 = 7;
    private static final int COL_ADDRESS3 = 8;
    private static final int COL_ADDRESS4 = 9;
    private static final int COL_ADDRESS5 = 10;
    private static final int COL_ADDRESS6 = 11;
//    private static final int COL_VOTE_TYPE = 22; not always present

    
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        String wardName = args[1];
        Date rollDate = formatRollDate.parse(args[2]);
        File logDir = new File (args[3]);
        
        PrintWriter newAddressLog = new PrintWriter (new File (logDir, "NewAddresses-" + wardName + ".log"));
        PrintWriter newPersonLog = new PrintWriter (new File (logDir, "NewPersons-" + wardName + ".log"));
	    
        ElectoralRollLoader loader = new ElectoralRollLoader (newPersonLog, newAddressLog);
        loader.begin(wardName, rollDate);
        
        CSVParser csv = new CSVParser (new FileInputStream (inFile));
        
        
        for (;;)
        {
            // cols aren't labelled
            String[] line = csv.getLine();

            if (line == null)
                break;
            
            if (line[COL_ROW_TYPE].trim().equals("U"))
                // more headings - ignore
                continue;
                                    
            String ref1 = line[COL_REF1].trim();
            String ref2 = line[COL_REF2].trim();
            
            String numbers[] = ref2.split("/");
            
            int voterNumber = Integer.valueOf(numbers[0]);
            
            Integer voterNumberSuffix = null;
            
            if (numbers.length > 1)
                voterNumberSuffix = Integer.valueOf(numbers[1]);
            
            
            loader.beginEntry(ref1, voterNumber, voterNumberSuffix);
            
            loader.putName (line[COL_FIRST_NAMES].trim(), 
                            line[COL_SURNAME].trim());
            
            loader.putAddress(line[COL_ADDRESS1].trim(), 
                              line[COL_ADDRESS2].trim(), 
                              line[COL_ADDRESS3].trim(), 
                              line[COL_ADDRESS4].trim(), 
                              line[COL_ADDRESS5].trim(), 
                              line[COL_ADDRESS6].trim());
            
            Date date18th = null;
            String str18th = line[COL_BIRTHDAY_18].trim();
            if (str18th.length() > 0)
                date18th = format18thDate.parse(str18th);
            
            if (date18th != null)
                loader.put18thBirthday (date18th);
            
            loader.endEntry();
        }
        
        csv.close();

        loader.end();
        
        newAddressLog.close();
        newPersonLog.close();
	}	
} 